package com.hua.yuan.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.Toast;

import cm.hua.yuan.common.utils.AppContextManager;

public class ToastUtil {
    private static Toast toast;
    @SuppressLint("StaticFieldLeak")
    private static View view;

    private ToastUtil() {
    }

    public static void showShortToast(CharSequence msg) {
        showToast(msg, Toast.LENGTH_SHORT);
    }


    public static void showShortToast(int resId) {
        showToast(resId, Toast.LENGTH_SHORT);
    }

    public static void showLongToast(CharSequence msg) {
        showToast(msg, Toast.LENGTH_LONG);
    }

    public static void showLongToast(int resId) {
        showToast(resId, Toast.LENGTH_LONG);
    }


    @SuppressLint("ShowToast")
    private static void getToast(Context context) {
        if (toast == null) {
            toast = new Toast(AppContextManager.getInstance().getContext());
        }
        if (view == null) {
            view = Toast.makeText(AppContextManager.getInstance().getContext(), "", Toast.LENGTH_SHORT).getView();
        }
        toast.setView(view);
    }

    private static void showToast(CharSequence msg, int duration) {
        try {
            getToast(AppContextManager.getInstance().getContext());
            toast.setText(msg);
            toast.setDuration(duration);
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void showToast(int resId, int duration) {
        try {
            if (resId == 0) {
                return;
            }
            getToast(AppContextManager.getInstance().getContext());
            toast.setText(resId);
            toast.setDuration(duration);
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

